
using System;
using System.ComponentModel;
using System.Windows;
using System.Collections;

using needle.UI.Controls.Month;

namespace needle.Appointments {

    /// <summary>
    /// Represents a collection of DateItem objects
    /// </summary>
    public class AppointmentsSelected : ReadOnlyCollectionBase {
        #region Class Data

        /// <summary>
        /// The Calendar that owns this DateItemCollection
        /// </summary>
        private MonthCalendar owner;

        #endregion


        #region Constructor

        public AppointmentsSelected(MonthCalendar owner)
            : base() {
            if (owner == null)
                throw new ArgumentNullException("owner");

            this.owner = owner;
        }

        public AppointmentsSelected(MonthCalendar owner, AppointmentsSelected dates)
            : this(owner) {

            if (owner == null)
                throw new ArgumentNullException("owner");

            this.owner = owner;

            this.Add(dates);
        }

        #endregion

        #region Methods


        public void Add(Appointment value) {
            int index;

            index = this.IndexOf(value);
            if (index == -1)
                this.InnerList.Add(value);
            else
                this.InnerList[index] = value;
        }

        public void AddRange(Appointment[] dates) {
            if (dates == null)
                throw new ArgumentNullException("dates");

            for (int i = 0; i < dates.Length; i++) {
                this.Add(dates[i]);
            }
        }

        public void Add(AppointmentsSelected dates) {
            if (dates == null)
                throw new ArgumentNullException("dates");

            for (int i = 0; i < dates.Count; i++) {
                this.Add(dates[i]);
            }
        }

        public void Clear() {
            while (this.Count > 0) {
                this.RemoveAt(0);
            }
        }

        public bool Contains(Appointment date) {
            return (this.IndexOf(date) != -1);
        }

        public int IndexOf(Appointment date) {

            for (int i = 0; i < this.Count; i++) {
                if (this[i] == date) {
                    return i;
                }
            }

            return -1;
        }

        public void Remove(Appointment value) {

            this.InnerList.Remove(value);

        }

        public void RemoveAt(int index) {
            this.Remove(this[index]);
        }

        public void Move(Appointment value, int index) {
            if (index < 0) {
                index = 0;
            }
            else if (index > this.Count) {
                index = this.Count;
            }

            if (!this.Contains(value) || this.IndexOf(value) == index) {
                return;
            }

            this.InnerList.Remove(value);

            if (index > this.Count) {
                this.InnerList.Add(value);
            }
            else {
                this.InnerList.Insert(index, value);
            }

        }

        public void MoveToTop(Appointment value) {
            this.Move(value, 0);
        }


        public void MoveToBottom(Appointment value) {
            this.Move(value, this.Count);
        }

        #endregion

        #region Properties

        public virtual Appointment this[int index] {
            get {
                Appointment d = (Appointment)this.InnerList[index];
                return d;
            }
        }

        #endregion

    }

}